/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.shailan.projects.webfea;

import com.shailan.projects.webfea.grid.Grid;
import com.shailan.util.fem.Model;
import com.shailan.util.fem.node.Node;
import com.shailan.util.fem.element.Truss_3D;
import com.sun.opengl.util.GLUT;
import java.util.Enumeration;
import javax.media.opengl.*;
import javax.media.opengl.glu.GLU;

/**
 *
 * @author MG
 */
public class ModelViewer {

    private Model model;
    private Grid grid;
    public boolean toggleGrid = true;
    public boolean toggleAxes = false;
    private GLUT glut = new GLUT();
    private GLU glu = new GLU();

    /**
     * @return the model
     */
    public Model getModel() {
        return model;
    }

    /**
     * @param model the model to set
     */
    public void setModel(Model model) {
        this.model = model;
    }

    /**
     * draw grid parametrically
     * depend on the nx,dx,ny,dy
     */
    public void drawGrid(GL gl) {
        gl.glLineWidth(1.0f);
        gl.glColor3f(0.5f, 0.5f, 0.5f);
        for (int i = -grid.ny; i < grid.ny; i++) {
            gl.glBegin(GL.GL_LINE_STRIP);
            for (int j = -grid.nx; j < grid.nx; j++) {
                gl.glVertex3f(j * grid.dx, i * grid.dy, 0);
            }
            gl.glEnd();
        }

        for (int i = -grid.nx; i < grid.nx; i++) {
            gl.glBegin(GL.GL_LINE_STRIP);
            for (int j = -grid.ny; j < grid.ny; j++) {
                gl.glVertex3f(i * grid.dx, j * grid.dy, 0);
            }
            gl.glEnd();
        }
    }

    public void draw(GL gl) {
        if (toggleGrid) {
            drawGrid(gl);
        }
        gl.glColor3f(0.0f, 1.0f, 0.0f);
        gl.glPointSize(3.0f);
        gl.glLineWidth(1.0f);
        int counter = 0;
        //DRAW NODES
        
        gl.glBegin(GL.GL_POINTS);
        
        while (counter < model.getNodesNum()) {
            if (counter > 0) { 
                gl.glTranslatef(model.getNodeX(counter), model.getNodeY(counter), model.getNodeZ(counter));
                glut.glutSolidSphere(0.03f, 8, 8);
                gl.glTranslatef(-model.getNodeX(counter), -model.getNodeY(counter), -model.getNodeZ(counter));
            }
            counter++;
        }
        gl.glEnd();
        //DRAW ELEMENTS
        gl.glColor3f(0.5f, 0.5f, 0.5f);
        gl.glLineWidth(2.0f);
        
        counter = 0;
        gl.glBegin(GL.GL_LINES);
        while (counter < model.getFramesNum()) {
            if (counter > 0) {
                int ni=model.getFrameNodeI(counter);
                int nj=model.getFrameNodeJ(counter);
                gl.glVertex3f(model.getNodeX(ni), model.getNodeY(ni), model.getNodeZ(ni));
                gl.glVertex3f(model.getNodeX(nj), model.getNodeY(nj), model.getNodeZ(nj));
            }
            counter++;
        }
        gl.glEnd();

    }

    

    /**
     * @return the grid
     */
    public Grid getGrid() {
        return grid;
    }

    /**
     * @param grid the grid to set
     */
    public void setGrid(Grid grid) {
        this.grid = grid;
    }
}
